package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.HelpInc;
import com.cmcc.aregister.cs.model.Appointment;
import com.cmcc.aregister.cs.model.DataUtilEntity;
import com.cmcc.aregister.cs.util.Utils;

public class HaoyuanTipAdapter extends BaseAdapter {
    private Context context;
    public List<Appointment> list;
    HelpInc helpInc;
    private static final String TAG = "HaoyuanTipAdapter";

    public HaoyuanTipAdapter(Context context, List<Appointment> list,
        HelpInc helpInc) {
        this.context = context;
        this.list = list;
        this.helpInc = helpInc;
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.a_haoyuan_tip_detail, null);
            holder.order_status = (TextView) convertView
                .findViewById(R.id.order_status);
            holder.remind_type = (TextView) convertView
                .findViewById(R.id.remind_type);
            holder.order_info = (TextView) convertView
                .findViewById(R.id.order_info);
            holder.appoint_start_time = (TextView) convertView
                .findViewById(R.id.appoint_start_time);
            holder.appoint_stop_time = (TextView) convertView
                .findViewById(R.id.appoint_stop_time);
            holder.can_appoint = (TextView) convertView
                .findViewById(R.id.can_appoint);
            holder.order_btn = (Button) convertView
                .findViewById(R.id.order_btn);
            holder.order_btn.setOnClickListener(mOnClickListener);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        Appointment app = list.get(position);
        Log.i(TAG, "位置:" + position + "  状态:" + app.yuyue_state);
        holder.remind_type.setText(Utils.getHaoyuanTipType(app.remind_type));
        holder.order_status.setText(Utils
            .getHaoyuanTipTaskStatus(app.yuyue_state));
        holder.order_info.setText(app.unit_name + "\n" + app.dep_name + " "
            + app.doctor_name);
        holder.appoint_start_time.setText(app.appoint_start_time);
        holder.appoint_stop_time.setText(app.appoint_stop_time);
        if (app.yuyue_state.equals("0")) {
            holder.order_btn.setText("取消任务");
            holder.order_btn.setVisibility(View.VISIBLE);
            DataUtilEntity pData = new DataUtilEntity();
            pData.position = position;
            holder.order_btn.setTag(pData);
        }
        else {
            holder.order_btn.setVisibility(View.GONE);
        }
        holder.can_appoint.setText(Utils.getCanAppoint(app.can_appoint));
        return convertView;
    }

    static class ViewHolder {
        TextView remind_type;
        TextView order_info;
        TextView order_status;
        TextView appoint_start_time;
        TextView appoint_stop_time;
        TextView can_appoint;
        Button order_btn;
    }

    OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            DataUtilEntity mData = (DataUtilEntity) v.getTag();
            helpInc.notifyUI(mData.position);
        }
    };
}